using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Microsoft.Win32;

namespace Common
{
    /// <summary>
    /// 
    /// </summary>
    public class EventLogUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="logName"></param>
        /// <param name="source"></param>
        public static void CreateEventLog(string logName, string source)
        {
            
            EventLog hoksoftLog;
            hoksoftLog = new EventLog();
            hoksoftLog.Log = logName;

            // set default event source (to be same as event log name) if not passed in
            if ((source == null) || (source.Trim().Length == 0))
            {
                source = logName;
            }

            hoksoftLog.Source = source;

            string keyName = @"SYSTEM\CurrentControlSet\Services\EventLog\" + logName;

            RegistryKey rkEventSource = Registry.LocalMachine.OpenSubKey(keyName + @"\" + source);

            // Check whether key exists
            if (rkEventSource == null)
            {
                Registry.LocalMachine.CreateSubKey(keyName + @"\" + source);
            }

            object eventMessageFile = rkEventSource.GetValue("EventMessageFile");

            if (eventMessageFile == null)
            {
                RegistryKey dotNetFrameworkSettings = Registry.LocalMachine.OpenSubKey(
                    @"SOFTWARE\Microsoft\.NetFramework\");

                if (dotNetFrameworkSettings != null)
                {
                    object dotNetInstallRoot = dotNetFrameworkSettings.GetValue(
                        "InstallRoot",
                        null,
                        RegistryValueOptions.None);

                    if (dotNetInstallRoot != null)
                    {
                        string eventMessageFileLocation =
                            dotNetInstallRoot.ToString() +
                            "v" +
                            System.Environment.Version.Major.ToString() + "." +
                            System.Environment.Version.Minor.ToString() + "." +
                            System.Environment.Version.Build.ToString() +
                            @"\EventLogMessages.dll";

                        if (System.IO.File.Exists(eventMessageFileLocation))
                        {
                            rkEventSource = Registry.LocalMachine.OpenSubKey(
                                keyName + @"\" + source,
                                true);

                            rkEventSource.SetValue(
                                "EventMessageFile",
                                eventMessageFileLocation,
                                RegistryValueKind.String);
                        }
                    }
                }

                dotNetFrameworkSettings.Close();
            }

            rkEventSource.Close();
        }
    }
}
